//
//  iTweet
//
//  Created by Dang Duc Nam (namk42c@yahoo.com) on 5/12/09.
//  Copyright 2009 Unitech. All rights reserved.
//


#import "TwitterRepliesViewController.h"
#import "iTweetAppDelegate.h"
#import "Status.h"
@implementation TwitterRepliesViewController

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/


- (void)viewDidLoad {
	UIBarButtonItem *refreshButton = [[[UIBarButtonItem alloc]
									   initWithTitle:NSLocalizedString(@"RefreshKey", @"")
									   style:UIBarButtonItemStyleBordered
									   target:self
									   action:@selector(doRefesh:)] autorelease];
	self.navigationItem.rightBarButtonItem = refreshButton;
	
	UIBarButtonItem *nextPageButton = [[[UIBarButtonItem alloc]
										initWithTitle:NSLocalizedString(@"NextPageKey", @"")
										style:UIBarButtonItemStyleBordered
										target:self
										action:@selector(doNextPage:)] autorelease];
	self.navigationItem.leftBarButtonItem = nextPageButton;
	
	self.navigationItem.title = NSLocalizedString(@"RepliesViewKey", @"");
	
	appDelegate = (iTweetAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSString *itemsCountValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"items_preference"];
	double countDouble = [itemsCountValue doubleValue];
	countInt = (int)(countDouble + (countDouble>0 ? 0.5 : -0.5));
	page = 1;
	[itemsCountValue release];
    [super viewDidLoad];
}



- (void)viewWillAppear:(BOOL)animated {
	//only fetch data when list empty
	if ([appDelegate.received_replies count] == 0)
		[appDelegate getReplies:page count:countInt refresh:NO];

    [super viewWillAppear:animated];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([appDelegate isDataSourceAvailable] == NO) {
        return 1;
    }
    if ([appDelegate isServiceAvailable] == NO) {
        return 1;
    }	
	return [appDelegate.received_replies count];
}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
	CGRect CellFrame = CGRectMake(0, 0, 320, 48);
	CGRect ImageFrame = CGRectMake(0, 0, 48, 48); //Image
	
	CGRect Label1Frame = CGRectMake(50, 0, 270, 30); //Tweet Status
	CGRect Label2Frame = CGRectMake(50, 30, 150, 18); // Friend Name
	CGRect Label3Frame = CGRectMake(200, 30, 120, 18);	// Date Time
	
	UIImageView *itemImageView;
	UILabel *lblTemp;
	
	
	UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
	
	//Initialize Label with tag 1.
	lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
	lblTemp.tag = 1;
	lblTemp.font = [UIFont boldSystemFontOfSize:12];
	lblTemp.textColor = [UIColor whiteColor];
	lblTemp.backgroundColor = [UIColor clearColor];
	lblTemp.numberOfLines = 0;
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	//Initialize Label with tag 2.
	lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
	lblTemp.tag = 2;
	lblTemp.font = [UIFont boldSystemFontOfSize:10];
	lblTemp.textColor = [UIColor lightGrayColor];
	lblTemp.backgroundColor = [UIColor clearColor];
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	//Initialize Label with tag 3.
	lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
	lblTemp.tag = 3;
	lblTemp.font = [UIFont boldSystemFontOfSize:10];
	lblTemp.textColor = [UIColor lightGrayColor];
	lblTemp.backgroundColor = [UIColor clearColor];
	[cell.contentView addSubview:lblTemp];
	[lblTemp release];
	
	//Initialize Label with tag 4.
	UIButton *btnTemp = [[UIButton alloc] initWithFrame:ImageFrame];
	btnTemp.tag = 4;
	btnTemp.backgroundColor = [UIColor clearColor];
	[btnTemp addTarget:self action:@selector(doProfile:) forControlEvents:UIControlEventTouchUpInside];	
	[cell.contentView addSubview:btnTemp];
	[cell.contentView bringSubviewToFront:btnTemp];
	[btnTemp release];
	
	//Initialize Label with tag 5.
	itemImageView = [[UIImageView alloc] initWithFrame:ImageFrame];
	itemImageView.tag = 5;
	[cell.contentView addSubview:itemImageView];
	[cell.contentView bringSubviewToFront:itemImageView];
	
	return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if ([appDelegate isDataSourceAvailable] == NO) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DefaultTableViewCell"] autorelease];
        cell.text = NSLocalizedString(@"NetworkErrorKey", @"");
		cell.font = [UIFont boldSystemFontOfSize:12];
		cell.textColor = [UIColor whiteColor];
		cell.accessoryType = UITableViewCellAccessoryNone;
		return cell;
	}
	if ([appDelegate isServiceAvailable] == NO) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DefaultTableViewCell"] autorelease];
        cell.text = NSLocalizedString(@"ServiceUnavailableKey", @"");
		cell.font = [UIFont boldSystemFontOfSize:12];
		cell.textColor = [UIColor whiteColor];
		cell.accessoryType = UITableViewCellAccessoryNone;
		return cell;
	}
	
	if(cell == nil)
		cell = [self getCellContentView:CellIdentifier];
	
	UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
	UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
	UILabel *lblTemp3 = (UILabel *)[cell viewWithTag:3];
	UIImageView *imageView = (UIImageView *)[cell viewWithTag:5];
	
	//First get the dictionary object
    Status *cellValue = [appDelegate.received_replies objectAtIndex:indexPath.row];
	
	lblTemp1.text = cellValue.text;
	lblTemp2.text = cellValue.name;
	
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]  autorelease];
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
	NSString *formattedDateString = [dateFormatter stringFromDate:cellValue.created_at];
	
	lblTemp3.text = formattedDateString;
	//get the image from received_images_small using userID
	imageView.image = [appDelegate.received_images_small objectForKey:cellValue.userID];
	
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

